/*
 * AddressBuilder.java
 *
 * Created on 02 July 2007, 08:11
 *
 * Copyright (C)
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

package com.grubby.databean.prebuilt;

import com.grubby.generators.NameGenerator;
import com.grubby.generators.RandomGenerators;
import java.util.Random;

/**
 * Generates random pseudo-realistic <AddressDatabean>es.
 * <P>
 * The <AddressDatabean>es have random house numbers, use surnames (@see NameGenerator)
 * as the basis of street, area and town names, and generate random postcodes
 * that should be valid.
 *
 * @author A. HARMEL-LAW. E: andrew.harmel.law@gmail.com
 * @version 0.1
 */
public class AddressBuilder {

    private static NameGenerator nameGenerator;

    private static Random random;
    
    private static String nameFileLoc = ".";

    public static final String[] streetTypes = {"LANE", "STREET", "CLOSE", "PARK"};

    public static final String[] townTypes = {"SHIRE", "TOWN", "VILLE", " PARK", "FIELD", "CASTLE"};

    /** Start the primary keys here and count them as they are used */
    public static long pkCounter = 0;
    
    /**
     * Constructor.  <Random> object used by this class is generated without a
     * seed.
     * @param The path to the Names file.
     */
    public AddressBuilder(String aNameFileLoc) {
        
        random = new Random(System.currentTimeMillis());
        nameGenerator = new NameGenerator(aNameFileLoc);
        
    }


    /**
     * Constructor.  <Random> object is instantiated with the provided seed
     * @param The path to the Names file.
     * @param randomSeed The number with which to seed the <Random>.
     * <P>
     * NOTE: <Random> is not truly random.  If you provide the same seed, the
     * same set of addresses will be generated.
     */
    public AddressBuilder(String aNameFileLoc, long aRandomSeed) {

        random = new Random(aRandomSeed);
        nameGenerator = new NameGenerator(aNameFileLoc);
    }


    /**
     * Generates a new <AddressDatabean> and returns it.
     * @return The generated <AddressDatabean>
     */
    public static AddressDatabean getAddress() {

        // 50% chance of having a 3rd line
        return new AddressDatabean((random.nextInt(150) + 1) + " " 
                    + nameGenerator.nextRandomSurname() + " " 
                    + RandomGenerators.getRandomStringFromArray(streetTypes), 
                nameGenerator.nextRandomSurname() 
                    + RandomGenerators.getRandomStringFromArray(townTypes), 
                (random.nextInt(100) < 50) ? nameGenerator.nextRandomSurname() + 
                    RandomGenerators.getRandomStringFromArray(townTypes) : null, 
                null, 
                null, 
                RandomGenerators.nextRandomPostcode(), 
                null);
        
    }

//    /**
//     * Generates a random postcode as a <String> and returns it.
//     * @return The generated postcode.
//     */
//    public String nextRandomPostcode() {
//
//        // Valid formats:
//        // ^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]?)
//        // ({1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$
//        //
//        // Note: this won't generate all valid formats, but a significant
//        // subset.
//        StringBuffer postcodeBuffer = new StringBuffer();
//        postcodeBuffer.append(RandomGenerators.getRandomCharFromString("ABCDEFGHIJKLMNOPRSTUWYZ"));
//        postcodeBuffer.append(RandomGenerators.getRandomCharFromString("ABCDEFGHKLMNOPRSTUWY"));
//        postcodeBuffer.append(RandomGenerators.getRandomCharFromString("0123456789"));
//        postcodeBuffer.append(RandomGenerators.getRandomCharFromString("ABEHMNPRVWXY"));
//        postcodeBuffer.append(' ');
//        postcodeBuffer.append(RandomGenerators.getRandomCharFromString("0123456789"));
//        postcodeBuffer.append(RandomGenerators.getRandomCharFromString("ABDEFGHJLNOPQRSTUWXYZ"));
//        postcodeBuffer.append(RandomGenerators.getRandomCharFromString("ABDEFGHJLNOPQRSTUWXYZ"));
//        String postcode = postcodeBuffer.toString();
//
//        return postcode;
//        
//    }

}